package com.bsoft.gol.hcb.dao.evaluation;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import hcn.base.evaluation.EvaluationItem;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;

import java.util.List;
import java.util.Map;

/**
 * Created by mlm on 2015/11/30.
 * <p/>
 * 满意度评价项目
 */
@SsdevDao(serviceId = "evaluationItemDAOService")
public abstract class EvaluationItemDAO extends HibernateSupportDelegateDAO<EvaluationItem> {

    public EvaluationItemDAO() {
        this.setKeyField("id");
    }

    /**
     * 通过业务类型查询评价项目
     *
     * @param businessType
     * @return
     */
    @RpcService
    @DAOMethod(sql = "select item from EvaluationItem item,EvaluationDefine def where def.businessType=:businessType and def.id=item.defineId ")
    public abstract List<EvaluationItem> findListByBusinessType(@DAOParam("businessType") String businessType);

    /**
     * 通过评价定义id获得满意度评价项目
     *
     * @param defineId
     * @return
     */
    @RpcService
    public List<Map<String, Object>> getEvaluationItemByDefineId(final String defineId) {
        HibernateStatelessResultAction<List<Map<String, Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String, Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("select a.* from base_evaluationitem a  where a.define_id=:defineId order by a.order_num desc  ");
                SQLQuery query = ss.createSQLQuery(hsql.toString());
                query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                query.setParameter("defineId", defineId);
                List<Map<String, Object>> ls = (List<Map<String, Object>>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 通过业务类型查询评价项目
     * 0:首证，1追证
     *
     * @param businessType
     * @return
     */
    @RpcService
    public List<EvaluationItem> findAddListByBusinessType(final String businessType, final String occasion) {
        HibernateStatelessResultAction<List<EvaluationItem>> action = new AbstractHibernateStatelessResultAction<List<EvaluationItem>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("select item from EvaluationItem item,EvaluationDefine def "
                        + "where def.businessType=? and def.id=item.defineId and def.occasion=?");
                Query query = ss.createQuery(hsql.toString());
                query.setParameter(0, businessType);
                query.setParameter(1, occasion);
                List<EvaluationItem> ls = (List<EvaluationItem>) query.list();
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    @RpcService
    public EvaluationItem saveEvaluationItem(final EvaluationItem evaluationItem) {
        return this.save(evaluationItem);
    }
}
